package com.example.sharedpreferences;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化
        SharedPreferences shp = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = shp.edit();

        //写入
        editor.putInt("NUMBER",100);
        editor.apply(); //建议用这个 也可以用commit


        //读取
        int x = shp.getInt("NUMBER",0);
        String TAG = "myLog";
//        Log.d(TAG, "onCreate: " + x);


        //第二种初始化方式
        SharedPreferences shp2 = getSharedPreferences("MY_DATA",Context.MODE_PRIVATE);

        //使用DATA
//        getApplicationContext() // 全局app的顶级引用
        MyData myData = new MyData(getApplicationContext());
        myData.number = 1000;
        myData.save();

        int y = myData.load();
        Log.d(TAG, "onCreate: " + y);
    }
}